package com.chenzehe.alimama.html;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class LoginPage extends BasePage {

	private String loginUrl = "http://www.alimama.com/member/login.htm?spm=0.0.0.0.n1i4Sy";
	private String homeUrl = "http://www.alimama.com/index.htm";

	public LoginPage(WebDriver webDriver) {
		super(webDriver);
	}

	public boolean login(String username, String password) {
		navigate(loginUrl);
		initForm(username, password);
		submitForm();
		waitForNavigateTo(homeUrl);
		return true;
	}

	/**
	 * 提交表单登录
	 */
	private void submitForm() {
		getWebDriver().findElement(By.id("J_SubmitStatic")).submit();
	}

	/**
	 * 初始化表单数据
	 */
	private void initForm(String username, String password) {
		getWebDriver().switchTo().frame("taobaoLoginIfr");
		clearAndSendKeys(getWebDriver().findElement(By.id("TPL_username_1")),
				username);
		//把复选框设置为不选中状态
		getWebDriver().findElement(By.id("J_SafeLoginCheck")).click();
		clearAndSendKeys(getWebDriver().findElement(By.id("TPL_password_1")),
				password);
	}

}
